package com.lin.generate.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lin.generate.common.base.R;
import com.lin.generate.common.base.PageReqVO;
import com.lin.generate.common.exception.BusinessException;
import com.lin.generate.common.utill.EasyPoiUtil;
import com.lin.generate.common.utill.UUIdUtils;
import com.lin.generate.common.vo.request.DataSourceConfigReqVO;
import com.lin.generate.common.vo.response.DataSourceConfigRespVO;
import com.lin.generate.service.IZlDataSourceConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 数据源配置
 *
 * @author zhaolin
 * @date 2023-06-04
 */
@RestController
@Api(tags = "数据源配置管理")
@Slf4j
@RequestMapping(value = "/data-source-config")
public class ZlDataSourceConfigController {

    @Autowired
    IZlDataSourceConfigService iDataSourceConfigService;


    /**
     * 新增数据源配置
     *
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(httpMethod = "POST", value = "新增数据源配置")
    public R<Boolean> saveDataSourceConfig(@RequestBody DataSourceConfigReqVO vo) {
        return R.ok(iDataSourceConfigService.save(vo));
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除", notes = "根据id删除")
    public R<Boolean> deleteById(@RequestParam("id") Long id) {
        return R.ok(iDataSourceConfigService.removeById(id));
    }


    /**
     * 修改数据源配置
     *
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(httpMethod = "PUT", value = "修改数据源配置", notes = "修改数据源配置")
    public R<Boolean> updateByIdDataSourceConfig(@RequestBody DataSourceConfigReqVO vo) {
        return R.ok(iDataSourceConfigService.update(vo));
    }

    /**
     * 分页查询
     *
     * @param pageReqVO
     * @param vo
     * @param response
     * @return
     */
    @GetMapping("/pageList")
    @ApiOperation(httpMethod = "GET", value = "分页查询或导出数据源配置", notes = "分页查询或导出数据源配置")
    public R<IPage<DataSourceConfigRespVO>> pageList(PageReqVO pageReqVO, DataSourceConfigReqVO vo, HttpServletResponse response) {
        try {
            IPage<DataSourceConfigRespVO> resultPage = iDataSourceConfigService.pageList(pageReqVO, vo);
            if (pageReqVO.getOperType() == 0L) {
                return R.ok(resultPage);
            }
            List<DataSourceConfigRespVO> records = resultPage.getRecords();
            String fileName = UUIdUtils.generateUUID() + ".xls";
            String title = "数据源配置列表";
            EasyPoiUtil.exportExcel(records, title, title, DataSourceConfigRespVO.class, fileName, response);
            return null;
        } catch (BusinessException e) {
            return R.fail(e.getError(), null);
        }
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/getObject/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据id查询", notes = "根据id查询")
    public R<DataSourceConfigRespVO> selectById(@PathVariable("id") Long id) {
        return R.ok(iDataSourceConfigService.getObjectById(id));
    }
}
